package com.my.hotel.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.hotel.entity.Order;
import com.my.hotel.entity.serverPage.JdUser;
import com.my.hotel.service.ClientService;
import com.my.hotel.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/orderPage")
    public String getPage(Model model, Integer pageNo){
        if(pageNo==null){
            pageNo=1;
        }
        PageHelper.startPage(pageNo,6);
        List<Order> list = orderService.getList();
        for (Order order : list) {
            System.out.println(order);
        }
        PageInfo<Order> page = new PageInfo<>(list);
        model.addAttribute("page",page);
        return "orderPage";
    }
    @RequestMapping("/changeStatus")
    public String changeStatus(int id,int status,Model model){
        Order order = new Order();
        order.setId(id);
        order.setOrderStatus(String.valueOf(status));
        orderService.changeStatus(order);
        return getPage(model);
    }
    @RequestMapping("/changeStatus1")
    public String changeStatus1(int id, int status, Model model, HttpSession session){
        Order order = new Order();
        order.setId(id);
        order.setOrderStatus(String.valueOf(status));
        orderService.changeStatus(order);
        JdUser userInfo = (JdUser) session.getAttribute("userInfo");
        return getPage1(userInfo.getId(),model);
    }
    @RequestMapping("myOrder")
    public String getMyOrder(Integer userId,Integer pageNo,Model model){
        System.out.println(userId);
        if(pageNo==null){
            pageNo=1;
        }
        PageHelper.startPage(pageNo,6);
        List<Order> list=orderService.getMyOrder(userId);
        for (Order order : list) {
            System.out.println(order);
        }
        PageInfo<Order> page = new PageInfo<>(list);
        model.addAttribute("page",page);
        return "serverPage/order";
    }

    public String getPage(Model model){
        return getPage(model,1);
    }
    public String getPage1(Integer userId,Model model){
        return getMyOrder(userId,1,model);
    }
}
